#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "usart.h"
#include "uart.h"
#include "w25q128.h"
#include "flash_on_chip.h"
#include "ota.h"
#define UART1_BUFF_MAX 1024 // UART1接收缓冲区大小
#define UART3_BUFF_MAX 1024 // UART1接收缓冲区大小
uint8_t uart1_rx_buff[UART1_BUFF_MAX] = {0}; // UART1接收缓冲区
uint8_t uart3_rx_buff[UART3_BUFF_MAX] = {0}; // UART1接收缓冲区
uint8_t ota_flag = 0;
/**
 * @brief 重定向printf函数到UART
 *
 * @param ch 字符
 * @param fp 文件指针
 * @return int 返回EOF表示错误
 */
int fputc(int ch, FILE *fp)
{
	HAL_StatusTypeDef ret;
	ret = HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,HAL_MAX_DELAY);
	if(ret != HAL_OK)
	{
		return EOF;
	}
	return 0;
}


/**
 * @brief  UART中断接收初始化
 * @note   使用HAL_UARTEx_ReceiveToIdle_IT函数进行中断接收
 * @retval None
 */
void uart_interrupt_init(void)
{
	HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart1_rx_buff,sizeof(uart1_rx_buff));
	HAL_UARTEx_ReceiveToIdle_IT(&huart3,(uint8_t *)uart3_rx_buff,sizeof(uart3_rx_buff));
}
/**
 * @brief UART接收完成回调函数
 *
 * @param huart UART句柄
 * @param Size 接收到的数据大小
 */
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if(huart == &huart1)
	{
		UART_LOG_INFO("uart1 interrupt\r\n");
		uart_rx_data(uart1_rx_buff,Size);			
	}
	// 串口3用来接收ota固件包
	if(huart == &huart3)
	{
		uart_ota_data(uart3_rx_buff,Size);
	}
	uart_interrupt_init();
	
}
void uart_test(void)
{
	uart_interrupt_init();
	return;
}
